<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Model;

use Carbon\Carbon;
use Qbhy\HyperfAuth\AuthAbility;
use Qbhy\HyperfAuth\Authenticatable;

use function Hyperf\Support\env;

/**
 * @property int $id
 * @property string $name 用户名
 * @property string $email 邮箱
 * @property string $account 登录账号
 * @property string $email_verified_at
 * @property string $password 密码
 * @property string $remember_token
 * @property int $login_count 登录次数
 * @property string $last_login_at 最后登录时间
 * @property int $gender 性别
 * @property string $telephone 电话
 * @property string $address 地址
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property int $status 状态
 * @property string $avatar 头像
 */
class User extends Model implements Authenticatable
{
    use AuthAbility;

    /**
     * The table associated with the model.
     */
    protected ?string $table = 'users';

    /**
     * The attributes that are mass assignable.
     */
    // protected array $fillable = [];
    protected array $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     */
    protected array $hidden = [
        'password',
        'salt',
    ];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'login_count' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'setting' => 'json', 'unread' => 'json'];

    // use AuthAbility;后不需要实现getId方法
    // public function getId()
    // {
    //     // TODO: Implement getId() method.
    //     return $this->getKey();
    // }

    // use AuthAbility;后不需要实现retrieveById方法
    // public static function retrieveById($key): ?Authenticatable
    // {
    //     // TODO: Implement retrieveById() method.
    //     return self::find($key);
    // }

    /**
     * 头像路径处理.
     *
     * @param string $value
     * @return string
     */
    public function getAvatarAttribute($value)
    {
        if (! str_contains($value, 'http')) {
            return env('STATIC_FILE_URL') . $value;
        }
        return $value;
    }

    public function getBgFilePathAttribute($value): string
    {
        if (empty($value)) {
            return '';
        }
        if (! str_contains($value, 'http')) {
            return env('STATIC_FILE_URL') . $value;
        }
        return $value;
    }

    public function getMomentBgFilePathAttribute($value): string
    {
        if (empty($value)) {
            return '';
        }
        if (! str_contains($value, 'http')) {
            return env('STATIC_FILE_URL') . $value;
        }
        return $value;
    }
}
